<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/26
 * Time: 10:16
 */

namespace Goods\Controller;

use Admin\Common\CommonController;

/**
 * Class ProductController
 * @package Goods\Controller
 * 货品管理
 */
class ProductController extends CommonController
{
    /**
     * 货品列表
     */
    public function lists(){
        //查找全部的货品数据
        $products=M('product')
            ->join("sp_goods ON sp_product.gid=sp_goods.gid")
            ->field("sp_goods.gname,sp_product.*")
            ->select();
        $this->assign('products',$products);
//        dd($products);
    $this->display();
    }

    /**
     * 货品删除
     */
    public function  del(){
        $pcid=I('get.pcid');
        $res=M('product')->where("pcid=$pcid")->delete();
        if($res){
            $this->success('删除成功');die;
        }else{
            $this->error('删除失败');die;
        }
    }
    /**
     * 货品添加
     */
    public function add(){
       $gid=I('get.gid');
        if(IS_POST){
//             dd($_POST);die;
            $_POST['style']=json_encode($_POST['style'],JSON_UNESCAPED_UNICODE);
//            dd($_POST['style']);die;
            $res=D('product')->store($_POST);
            if($res){
               $this->success('添加成功',U('lists'));die;
            }else{
                $this->error('添加失败');die;
            }

        }
//        dd($gid);
        //查出商品的数据
        $good=current(M('goods')->where("gid=$gid")->getField('gid,gname,number,style'));
        $style=$good['style'];
        $style=json_decode($style,true);
        $arr=[];
        foreach($style as $k=>$v){
            foreach($v as $m=>$n){
              $arr[$m][]=$n;
            }
        }
//        dd($arr);
        $arr1=[];
        foreach($arr as $k=>$v){
          if(count($v)>=2){
             $arr1[]=$k;
          }
        }
//        dd($arr1);
//        dd($style);
        $arr2=[];
        foreach($arr as $k=>$v){
              if(in_array($k,$arr1)){
                $arr2[$k]=$v;
              }
        }
//        dd($arr2);
        //商品样式
        $this->assign('arr2',$arr2);
        $this->assign('good',$good);
//        dd($good);
        $this->display();
    }
}